﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.Master
{
    
    public partial class Template : PageBase
    {
        public string FJID { set; get; }
        AttachmentTemplateBO bo = new AttachmentTemplateBO();
        LogBO lbo = new LogBO();
        T_FileBO manager = new T_FileBO();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                //if (Request.QueryString["DID"] != null)
                //{
                //    LoadDep(Request.QueryString["DID"].ToString());
                //}
                LoadData();
            }
            if (Request.QueryString["deid"] != null)
            {
                dele(Request.QueryString["deid"].ToString());
            }
            if (Request.QueryString["deid"] != null)
            {
                DeleteFile(Request.QueryString["deid"].ToString());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileid"></param>
        private void DeleteFile(string fileid)
        {
            try
            {
                DataSet ds = manager.Getpath(int.Parse(fileid));
                string path = ds.Tables[0].Rows[0]["F_Detail"].ToString();

                if (manager.DeleteFile(fileid))
                {
                    // File.Delete(path);
                    AlertRedirect("Deleted successful", "Template.aspx");
                    //AlertBack("Deleted successful");
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void dele(string id)
        {
            int num = bo.Delete(int.Parse(id));
            lbo.WriteLog(CurrentUser.Id, "Delete Template");
            if (num > 0)
                AlertMessage("Deleted successful");
            LoadData();
        }

        private void LoadData()
        {
            DataSet ds = bo.GetList();
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        //private void LoadDep(string id)
        //{
        //    Model.AttachmentTemplate d = bo.GetModel(int.Parse(id));
        //    txtAttachment.Text = d.Attachmenttype;
        //    txtRemark.Text = d.REMARK;
        //    ddlTemplateAccepted.SelectedValue = d.SUPPLIERSTEMPLATEACCEPTED.ToString();
        //    ddlTemplateAvailable.SelectedValue = d.BDNTTEMPLATEAVAILABLE.ToString();
        //    cbLevel1.Checked = d.ISLEVEL1 == 1 ? true : false;
        //    cbLevel2.Checked = d.ISLEVEL2 == 1 ? true : false;
        //    cbLevel3.Checked = d.ISLEVEL3 == 1 ? true : false;
        //    Button1.Visible = true;
        //    FJID = d.UPLOADEDFILE;
        //    //btnUp.Visible = true;
        //    lblUp.Text="<input type='button'   class='button'  onclick='javascript:PopUpWindow(\"UploadFile.aspx?FJID="+d.UPLOADEDFILE+"\", 230, 160, 730, 350);'   value='Upload'>";
        //    DataSet ds = manager.GetFileList(d.UPLOADEDFILE);
        //    GridView2.DataSource = ds;
        //    GridView2.DataBind();
        //    Button1.Text = "Save";
        //}

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Model.AttachmentTemplate d = new Model.AttachmentTemplate();
        //    if (Request.QueryString["DID"] != null)
        //       d= bo.GetModel(int.Parse(Request.QueryString["DID"].ToString()));
        //    int num = 0;
        //    d.REMARK = txtRemark.Text;
        //    d.SUPPLIERSTEMPLATEACCEPTED = int.Parse(ddlTemplateAccepted.SelectedValue);
        //    d.BDNTTEMPLATEAVAILABLE = int.Parse(ddlTemplateAvailable.SelectedValue);
        //    d.ISLEVEL1 = cbLevel1.Checked ? 1 : 0;
        //    d.ISLEVEL2 = cbLevel2.Checked ? 1 : 0;
        //    d.ISLEVEL3 = cbLevel3.Checked ? 1 : 0;
        //    d.LASTMODIFIEDDATE = DateTime.Now;
        //    if (Button1.Text == "Add")
        //    {
        //        num = bo.Add(d);
        //        lbo.WriteLog(CurrentUser.Name, "Add Template:"+d.Attachmenttype);
        //    }
        //    else
        //    {
        //        d.ID = int.Parse(Request.QueryString["DID"].ToString());
        //        num = bo.Update(d);
        //        lbo.WriteLog(CurrentUser.Name, "Edit Template:"+d.Attachmenttype);
        //    }
        //    if (num > 0)
        //    {
        //        txtAttachment.Text = "";
        //        txtRemark.Text = "";
        //        Button1.Text = "Add";
        //        Button1.Visible = false;
        //        //AlertMessage("Save success");
        //    }
        //    LoadData();
        //}

    }
}